
// 轮播图显示--------------------------------------------------
function getList() {
    $.ajax({
      url: "/admin/swipers",
      type:"get",
      success: function(res) {
        // console.log(res)
        if (res.status === 0) {
          let str = "";
      
          $.each(res.data, function(index, value) {
              if (value.swiperstatus === 1) {
                str += 
                `<tr>
                <td>${value.id}</td>
                <td>
                  <img src="http://localhost:8888/uploads/${value.swiperimg}">
                </td>
                <td>${value.swiperimg}</td>
                <td>
                  <button class="layui-btn  layui-btn-sm" data-status="2" myid="${value.id}" id="yes">√</button>

                    </td>
                <td><button myid="${value.id}" class="layui-btn  layui-btn-danger layui-btn-sm delete" lay-event="delete">删除</button></td>
              </tr>` 
              } else {
                str += 
                `<tr>
                <td>${value.id}</td>
                <td>
                  <img src="http://localhost:8888/uploads/${value.swiperimg}">
                </td>
                <td>${value.swiperimg}</td>
                <td>
                  <button class="layui-btn  layui-btn-sm" data-status="1" myid="${value.id}" id="yes" style=" background-color: #2F4056">x</button>
                
                    </td>
                <td><button myid="${value.id}" class="layui-btn  layui-btn-danger layui-btn-sm delete" lay-event="delete">删除</button></td>
              </tr>` 
              }
            
          });
          $("tbody").html(str);
        }
      }
    });
  }
  getList();
// -------------------------------------------------------------删除

// 1.事件委托
$("tbody").on("click", ".delete", function(e) {
    // 2.获取id；初始化的时候，需要把id和删除按钮绑定在一起；
   let id = $(e.target).attr("myid");
    // 3.发送删除请求
    $.ajax({
      type:"delete",
      url: "/admin/swipers/" + id,
      success: function(res) {
        // 4.成功后：重新加载列表
        layer.msg(res.message);
        if (res.status === 0) {
          // 删除成功，重新渲染
          getList();
        }
      }
    })
  });
//切换状态----------------------------------------------------
$("tbody").on("click", "#yes", function(e) {
   //获取id；初始化的时候，需要把id和删除按钮绑定在一起；
   let id = $(e.target).attr("myid");
   let flag = $(e.target).attr("data-status");
    $.ajax({
        type:"put",
        url: "/admin/swipers/" + id,
        data:{
          status: flag
        },
        success: function(res) {
          // 4.成功后：重新加载列表
          layer.msg(res.message);
          console.log(res);
          if (res.status === 0) {
           
            getList();
          }
        }
      })
    })





//上传文件-----------------------------------------------------
$("#upload").click(function(){
  $("#file").click()
})
$("#file").change(function(){
  let fd = new FormData()
  // console.log(this.files);
  $.each(this.files, function(index,value) {
    fd.append('swipers', value)
  })
  
  $.ajax({
    type: 'post',
    url: '/admin/swipers',
    data: fd,
    processData: false,
    contentType: false,
    success: function (res) {
      // console.log(res);
      layer.msg(res.message)
      if (res.status === 0) {
        getList()
      }
    }
  })
})